package com.fh.fruit.controller.config;

import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.fh.controller.base.BaseController;
import com.fh.entity.Page;
import com.fh.entity.system.User;
import com.fh.fruit.entity.goods.GoodsUnit;
import com.fh.fruit.service.config.ConfigManager;
import com.fh.util.Const;
import com.fh.util.DateUtil;
import com.fh.util.Jurisdiction;
import com.fh.util.PageData;

@Controller
@RequestMapping(value="/config")
public class ConfigController extends BaseController {
	
	String menuUrl = "config/list.do"; //版本信息地址(权限用)
	@Resource(name="configService")
	private ConfigManager configService;
	
	/**显示app版本信息列表
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/aliyunList")
	public ModelAndView listAppVersion(Page page,HttpSession httpSession)throws Exception{
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "cha")){return null;} //校验权限
		ModelAndView mv = this.getModelAndView();	
		PageData pd=this.getPageData();
		page.setPd(pd);
		List<Map<String,String>> aliList=configService.aliPaylistPage(page);
		mv.setViewName("farm/config/aliyun_list");
		mv.addObject("aliList", aliList);
		mv.addObject("pd", pd);
		mv.addObject("QX",Jurisdiction.getHC());	//按钮权限
		System.out.println("权限=========="+Jurisdiction.getHC());
		return mv;
	}
	
	/**修改阿里云支付配置使用状态
	 * @param out
	 * @throws Exception 
	*/
	@RequestMapping(value="/updateAliPay")
	@ResponseBody
	public String updateAliPay(HttpServletRequest requset) throws Exception{
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "edit")){return "0";} //校验权限
		logBefore(logger, Jurisdiction.getUsername()+"修改配置");
		PageData pd = new PageData();
		pd = this.getPageData();
		int num=configService.updateAliPayState(pd);
		if(num >0){
			return "1";
		}else{
			return "0";
		}
	}
	
	/**
	 * 去新增aliyun配置页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/goAddAli")
	public ModelAndView goAddG(Page page) throws Exception {
		if (!Jurisdiction.buttonJurisdiction(menuUrl, "add")) {
			return null;
		} // 校验权限
		ModelAndView mv = this.getModelAndView();
		mv.setViewName("farm/config/aliyun_edit");
		return mv;
	}
	
	/**
	 * 新增阿里云支付配置
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/saveAliyunConfig")
	public ModelAndView saveU(HttpServletRequest requset) throws Exception {
		if (!Jurisdiction.buttonJurisdiction(menuUrl, "add")) {
			return null;
		} // 校验权限
		logBefore(logger, Jurisdiction.getUsername() + "新增阿里云支付配置");
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		configService.saveAliyunConfig(pd);
		mv.setViewName("save_result");
		return mv;
	}
	
	
}
